<template>
  <!-- 基础配置页面 -->
  <div>
    <!-- {{ tableData }} -->
    <el-button type="primary" @click="toAdd">新增</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="值" align="center">
        <template slot-scope="scope" width="40">
          <el-image
            v-if="scope.row.val.indexOf('http://') !== -1"
            :src="scope.row.val"
            style="width: 40px; height: 40px"
          />
          <span v-else>{{ scope.row.val }}</span>
        </template>
      </el-table-column>
      <el-table-column label="介绍" align="center">
        <template slot-scope="scope">
          {{ scope.row.introduce }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <a class="green_info" @click="toEdit(scope.row)">编辑</a>
          <a class="red_info" @click="toDelect(scope.row.id)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="visible">
      <!-- 模态框 -->
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="介绍"
          :label-width="formLabelWidth"
          prop="introduce"
        >
          <el-input v-model="form.introduce" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-select v-model="value">
            <el-option label="文字" value="文字" />
            <el-option label="图片" value="图片" />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="value == '文字'"
          v-model="value"
          label="值"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.val" autocomplete="off" />
        </el-form-item>
        <el-form-item
          v-if="value == '图片'"
          v-model="value"
          label="图片上传"
          :label-width="formLabelWidth"
        >
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  baseConfig_findAll,
  baseConfig_deleteById,
  baseConfig_saveOrUpdate
} from '@/api/baseConfig'
import { showFileURL, uploadFileURL } from '@/utils/config'
// import { log } from '@antv/g2plot/lib/utils'
export default {
  data() {
    return {
      showFileURL,
      uploadFileURL,
      tableData: [],
      visible: false,
      formLabelWidth: '80px',
      form: {},
      imageUrl: '',
      value: '文字',
      title: '新增基础配置',
      rules: {
        name: [
          {
            required: true,
            message: '请输入轮播图名称',
            trigger: 'blur'
          }
        ],
        introduce: [
          {
            required: true,
            message: '请输入轮播图介绍信息',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.findAll()
  },
  mounted() {},
  methods: {
    // 提示
    tips() {
      this.$message({
        type: 'success',
        message: '更新成功!'
      })
    },
    async findAll() {
      const res = await baseConfig_findAll()
      this.tableData = res.data
    },
    toAdd() {
      this.findAll()
      this.imageUrl = ''
      this.form = {}
      this.visible = true
    },
    toSave() {
      // 获取dom的值，对值进行校验 form.name
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 校验通过，做自己的业务，保存
          // console.log("校验通过");
          // this.form;
          await baseConfig_saveOrUpdate(this.form)
          // console.log(res, "-----");
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify.success({
            title: '成功',
            // position: "top-left",
            // offset: 50,
            message: '新增成功'
          })
          this.findAll()
          // this.toClose();
          this.visible = false
        } else {
          // 校验不通过
          return false
        }
      })
      this.value = '文字'
    },

    toEdit(row) {
      this.title = '编辑基础配置'
      this.visible = true
      // this.findAll();
      this.form = { ...row }
      this.value = this.form.val.indexOf('http://') !== -1 ? '图片' : '文字'
      if (this.value === '图片') {
        this.imageUrl = this.form.val
      }
    },
    toDelect(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await baseConfig_deleteById({ id })
          this.findAll()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.val = this.showFileURL + res.data.id
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style lang="scss" scoped>
.sg {
  width: 200px;
  text-align: center;
}
</style>
